package com.pay.controller.api.frontend;

import com.pay.common.JsonResult;
import com.pay.common.ProcessAPI;
import com.pay.common.enums.ResultCode;
import com.pay.controller.Routes;
import com.pay.domain.entity.Bank;
import com.pay.lifecycle.repository.BankRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 查询银行信息
 * @author Evan
 * @date 2018/2/4
 */
@Controller
public class BankFrontendController {
    private Logger logger = LoggerFactory.getLogger(BankFrontendController.class);

    @Autowired
    private BankRepository bankRepository;

    /**
     * 通过银行名称获取bankCode
     * @param bankName
     * @return
     */
    @RequestMapping(value = Routes.GET_BANK_CODE, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getBankCode(HttpServletRequest request,String bankName) {
        ProcessAPI api = (result, validate) -> {
            logger.info("getBankCode bankName={}",bankName);
            Bank bank = bankRepository.findByBankName(bankName);
            if(bank == null){
                result.resultCode = ResultCode.NO_DATA.getCode();
                result.msg = ResultCode.NO_DATA.getMsg();
                return;
            }
            result.result = bank.getBankCode();
        };
        return api.process(request);
    }

    /**
     * 获取银行列表
     * @return
     */
    @RequestMapping(value = Routes.GET_BANK_LIST, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getBankList(HttpServletRequest request) {
        ProcessAPI api = (result, validate) -> {
            List<Bank>  bankList = bankRepository.findByShowStatus(1);
            if(bankList == null){
                result.resultCode = ResultCode.NO_DATA.getCode();
                result.msg = ResultCode.NO_DATA.getMsg();
                return;
            }
            result.result = bankList;
            result.size = Long.valueOf(bankList.size());
        };
        return api.process(request);
    }


}
